<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-8-25
 * Time: 15:04
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;

class DetailController extends AdminBaseController
{
    public function index()
    {
        $param    = $this->request->param();
        $u_nickname = isset($param['u_nickname']) ? $param['u_nickname'] : '';
        $u_phone = isset($param['u_phone']) ? $param['u_phone'] : '';
        $where['u_type'] = ['lt',100];
        if($u_nickname){ $where['u_nickname'] = ['like', "%$u_nickname%"];}else{ unset($param['u_nickname']);}
        if($u_phone){ $where['u_phone'] = ['like', "%$u_phone%"];}else{ unset($param['u_phone']);}
        $detail = Db::name('users_detail')
            ->where($where)
            ->order(['u_status'=>'DESC','id' => 'DESC'])
            ->paginate(20);
        $detail->appends($param);
        $page = $detail->render();
        $this->assign("u_nickname", $u_nickname);
        $this->assign("u_phone", $u_phone);
        $this->assign("detail", $detail);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function info()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $detail = DB::name('users_detail')->where(["id" => $id])->find();
        $this->assign("detail", $detail);
        return $this->fetch();
    }

    public function infoPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'u_nickname' => 'require|min:1|max:16',
                'u_sex' => 'number|between:0,2',
                'u_age' => 'require|number|between:0,126',
                'u_score' => 'require|number',
                'u_status' => 'number',
            ]);
            $validate->message([
                'u_nickname.require' => '用户昵称不可为空',
                'u_nickname.min' => '用户昵称最短不可低于1个字符',
                'u_nickname.max' => '用户昵称最长不可超过16个字符',
                'u_sex.number' => '请选择性别',
                'u_sex.between' => '性别错误',
                'u_age.require' => '请输入年龄',
                'u_age.number' => '年龄错误',
                'u_age.between' => '年龄不可超过126岁',
                'u_score.require' => '经验值不可为空',
                'u_score.number' => '经验值只能是数字',
                'u_status.number' => '用户状态错误',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                Db::name('users_detail')->update($data);
                $this->success('修改成功',url('detail/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }
    }

    public function enabled()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $shout = DB::name('users_detail')->where(["id" => $id])->find();
        switch ($shout['u_status']){
            case 10:
                DB::name('users_detail')->where(["id" => $id])->setField('u_status', 20);
                $this->success('您已关闭此预播');
                break;
            case 20:
                DB::name('users_detail')->where(["id" => $id])->setField('u_status', 10);
                $this->success('您已开启此预播');
                break;
            default:
                DB::name('users_detail')->where(["id" => $id])->setField('u_status', 20);
                $this->error('未知的预播状态,已置为停用');
        }
    }

    public function watch()
    {
        $param    = $this->request->param();
        $payment = Db::name('payment')->field('p.id,p.p_channel,p.p_price,p.p_receive,p.p_createtime,sl.s_name,sl.s_image')->alias('p')
            ->join('starList sl','p.p_receive = sl.s_udid')
            ->where('p.p_udid',$param['id'])
            ->order(['p.id' => 'DESC'])
            ->paginate(20);
        $payment->appends($param);
        $page = $payment->render();
        $this->assign("payment", $payment);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function obtain()
    {
        $param    = $this->request->param();
        $where['o.o_receive'] = $param['id'];
        $where['o.o_type'] = 20;
        $where['gc.g_status'] = ['egt',30];

        $obtain = Db::name('obtain')->field('o.id,sl.s_name,o.o_createtime,gl.g_name,gl.g_sponsor,gl.g_password,gl.g_phone,gl.g_url,gl.g_description,gl.g_validtime,gc.g_number,gc.g_status,gc.g_status,gc.g_edittime')->alias('o')
            ->join('StarList sl','sl.s_udid = o.o_udid')
            ->join('GiftList gl','o.o_money = gl.id')
            ->join('GiftCard gc','gc.g_glid = o.o_money AND gc.g_receive = o.o_receive')
            ->where($where)
            ->order(['o.id' => 'DESC'])
            ->paginate(20);
        $obtain->appends($param);
        $page = $obtain->render();
        $this->assign("obtain", $obtain);
        $this->assign("page", $page);
        return $this->fetch();
    }

}